<template>
  <div class="teacher-container">
    <div class="common-layout">
      <el-container class="layout-container">
        <el-aside class="sidebar">
          <div class="sidebar-content">
            <h5 class="sidebar-title">
              <div style="margin-left:20px;display:flex;align-items: center;float:left;vertical-align: middle;height:100%">
                <a :href="'/#/lession?courseid='+courseid+'&name='+name"><el-icon><Back /></el-icon></a>&nbsp;
              </div>&nbsp;
              {{name}}

            </h5>
            <el-menu
                default-active="2"
                class="el-menu-vertical-demo"
                background-color="#f8fafc"
                active-text-color="#4f46e5"
            >
              <el-menu-item index="1" class="menu-item">
                <a :href="'/#/classroom?courseid='+courseid+'&lessionid='+lessionid+'&name='+name">  <el-icon><Document /></el-icon>
                  <span>Material</span></a>
              </el-menu-item>
              <el-menu-item index="2" class="menu-item">
                <a :href="'/#/classroomaudio?courseid='+courseid+'&lessionid='+lessionid+'&name='+name">  <el-icon><VideoPlay /></el-icon>
                  <span>Recoding</span></a>
              </el-menu-item>
              <el-menu-item index="3" class="menu-item">
                <a :href="'/#/classroomscript?courseid='+courseid+'&lessionid='+lessionid+'&name='+name"><el-icon><Service /></el-icon>
                  <span>transcript</span></a>
              </el-menu-item>
            </el-menu>
          </div>

        </el-aside>

        <!-- Main Content -->
        <el-main class="main-content">
          <el-row>
            <div class="page-header">
              <!--            <el-col :span="24" class="topdiv">-->
<!--              <el-button :icon="Upload" type="primary" @click="showdialog()">upload</el-button>-->
              <!--            </el-col>-->
            </div>
            <el-col :span="24">

<!--              <div class="content-panel"  v-for="item in lessionfiledata.list">-->
<!--                <div class="content" style="height:40px;display: flex;align-items: center;">-->
<!--                  &lt;!&ndash; {{item.filename}}&nbsp;&nbsp;{{item.filesize}} &ndash;&gt;<label style="margin: auto;height:40px;display: inline;line-height: 40px;">{{item.createtime}}</label>-->
<!--                  &lt;!&ndash;controls="controls"&ndash;&gt;-->
<!--                  <video :src="serverurl+item.filepath" width="640" height="40" controls style="margin: auto;" >您当前浏览器不支持HTMT5视频播放</video>-->
<!--                </div>-->
<!--                <div class="button">-->
<!--                  &lt;!&ndash; <el-button type="primary" :icon="VideoPlay" circle @click="videoPlay(item)"/>     &ndash;&gt;-->
<!--                  <el-button type="danger" :icon="Delete" circle @click="doDelete(item.id)" v-if="lessionfiledata.isMyCourse"/>-->
<!--                </div>-->
<!--              </div>-->

              <div class="course-list">
                <div
                    class="course-card"
                    v-for="item in lessionfiledata.list"
                    :key="item.id"
                >
                  <div class="course-info">
                    <h3 class="course-name">{{item.createtime}}</h3>
                    <video :src="serverurl+item.filepath" width="640" height="80" controls style="margin: auto;" >您当前浏览器不支持HTMT5视频播放</video>
                    <!--                <span class="course-year" v-if="item.marjor">-->
                    <!--                  {{ getYearLabel(item.marjor) }}-->
                    <!--                </span>-->
                  </div>
                  <div class="course-actions">
                    <el-tooltip content="Delete Course" placement="top">
                      <el-button
                          type="danger"
                          :icon="Delete"
                          circle
                          @click="doDelete(item.id)"
                          class="action-btn"
                      />
                    </el-tooltip>
                  </div>
                </div>
              </div>


            </el-col>
          </el-row>

        </el-main>
      </el-container>
    </div>

  </div>
</template>
<script setup lang="ts">

//import TopHeader from '../../components/TopHeader.vue' // 引用Header组件
//import TopBottom from '../../components/TopBottom.vue' // 引用Header组件

import {
  Download,
  Delete,
  Edit,
  Message,
  Search,
  VideoPlay,
} from '@element-plus/icons-vue'
import { reactive,ref,onMounted} from 'vue'
import { useRoute,useRouter } from 'vue-router'
import { genFileId } from 'element-plus'
import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus'
import { uploadImage,download,isMyCourse } from '../../api/course.ts'
import { getMyLessionTransaudioList,deleteLessionTransaudio } from '../../api/lessiontransaudio.ts'

const route = useRoute()
const currentRoute = useRouter()
const courseid = route.query.courseid
const lessionid = route.query.lessionid
const name = route.query.name

const env = import.meta.env
const serverurl:string = env['VITE_APP_BASE_API']

//单门onelessionfile，增加和编辑使用
const onelessionfile = reactive({
    id:null,
    name:'',
    filename:'',
    filesize:'',
    filepath:'',
    courseid:courseid,
    lessionid:lessionid
})
//lessionfile列表
const lessionfiledata = reactive({
    list:[],
    isMyCourse:false
})

onMounted(() => {
  loaddata();
})

function loaddata()
{
  getMyLessionTransaudioList({"lessionid":lessionid}).then((res) => {
    if (res.code == 200)
    {
      lessionfiledata.list = res.data;
    }
  })
  isMyCourse({"courseid":courseid}).then((res) => {
    if (res.code == 200)
    {
      lessionfiledata.isMyCourse = res.data;
    }
  })
}

function videoPlay(item)
{

}


function doDelete(id)
{
  deleteLessionTransaudio({"id":id}).then((res) => {
    if (res.code == 200)
    {
      //更新下视图数据
      loaddata();
    }
  })
}
function doDownload(filepath,filename)
{
  download({"filepath":filepath}).then((res) => {
    downloadLink(res,filename);
  })

}

function downloadLink(data, title) {
  var elink = document.createElement("a");
  elink.download = title;
  elink.style.display = "none";
  var blob = new Blob([data]);
  elink.href = window.URL.createObjectURL(blob);
  document.body.appendChild(elink);
  elink.click();
  document.body.removeChild(elink);
}

</script>
<style lang="less" scoped>
.teacher-container {
  height: 100vh;
  background-color: #f8fafc;
}

.layout-container {
  height: 100%;
}

.sidebar {
  width: 220px !important;
  background-color: #f8fafc;
  border-right: 1px solid #e2e8f0;

  .sidebar-content {
    padding: 20px 0;

    .sidebar-title {
      color: #64748b;
      font-size: 16px;
      font-weight: 600;
      padding: 0 20px 15px;
      margin: 0;
      border-bottom: 1px solid #e2e8f0;
    }
  }

  .menu-item {
    margin: 5px 10px;
    border-radius: 6px;
    transition: all 0.3s ease;

    &:hover {
      background-color: #eef2ff !important;
      color: #4f46e5 !important;
    }

    &.is-active {
      background-color: #eef2ff !important;
      color: #4f46e5 !important;
      font-weight: 500;
    }
  }
}

.main-content {
  padding: 30px !important;
  background-color: white;
  border-radius: 12px 0 0 0;
  box-shadow: -2px 0 8px rgba(0, 0, 0, 0.05);
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;

  .page-title {
    color: #1e293b;
    font-size: 24px;
    margin: 0;
  }

  .add-course-btn {
    background-color: #4f46e5;
    border: none;
    padding: 10px 20px;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(79, 70, 229, 0.2);
    transition: all 0.3s ease;

    &:hover {
      background-color: #4338ca;
      transform: translateY(-2px);
    }

    .el-icon {
      margin-right: 8px;
    }
  }
}

.course-list {
  display: grid;
  grid-template-columns: 1fr;
  gap: 15px;
}

.course-card {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: white;
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  border: 1px solid #f1f5f9;

  &:hover {
    transform: translateY(-3px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    border-color: #e0e7ff;
  }

  .course-info {
    .course-name {
      font-size: 16px;
      font-weight: 600;
      color: #1e293b;
      margin: 0 0 5px 0;
    }

    .course-year {
      font-size: 13px;
      color: #64748b;
      background-color: #f1f5f9;
      padding: 3px 8px;
      border-radius: 4px;
    }
  }

  .course-actions {
    display: flex;
    gap: 10px;

    .action-btn {
      transition: all 0.2s ease;

      &:hover {
        transform: scale(1.1);
      }
    }
  }
}

.course-dialog {
  :deep(.el-dialog) {
    border-radius: 12px;
    overflow: hidden;
  }

  :deep(.el-dialog__header) {
    border-bottom: 1px solid #f1f5f9;
    padding: 20px;
    margin-right: 0;

    .el-dialog__title {
      font-weight: 600;
      color: #1e293b;
    }
  }

  :deep(.el-dialog__body) {
    padding: 20px;
  }

  //.dialog-input,
  .dialog-select {
    width: 100%;

    :deep(.el-input__inner) {
      border-radius: 8px;
      border: 1px solid #e2e8f0;
      height: 40px;

      &:focus {
        border-color: #4f46e5;
        box-shadow: 0 0 0 1px rgba(79, 70, 229, 0.2);
      }
    }
  }

  .status-radio {
    :deep(.el-radio__inner) {
      border-color: #cbd5e1;
    }

    :deep(.el-radio__input.is-checked .el-radio__inner) {
      background-color: #4f46e5;
      border-color: #4f46e5;
    }

    :deep(.el-radio__label) {
      color: #475569;
    }
  }

  .upload-btn {
    background-color: #f1f5f9;
    border: none;
    color: #4f46e5;
    border-radius: 8px;

    &:hover {
      background-color: #e0e7ff;
    }

    .el-icon {
      margin-right: 8px;
    }
  }

  .image-preview {
    margin-top: 15px;

    img {
      width: 100px;
      height: 100px;
      object-fit: cover;
      border-radius: 8px;
      border: 1px solid #e2e8f0;
    }
  }

  .dialog-footer {
    display: flex;
    justify-content: flex-end;
    gap: 10px;

    .cancel-btn {
      border-radius: 8px;
      border: 1px solid #e2e8f0;

      &:hover {
        color: #4f46e5;
        border-color: #c7d2fe;
      }
    }

    .confirm-btn {
      background-color: #4f46e5;
      border: none;
      border-radius: 8px;

      &:hover {
        background-color: #4338ca;
      }
    }
  }
}

@media (max-width: 768px) {
  .sidebar {
    width: 100% !important;
    height: auto !important;
    border-right: none;
    border-bottom: 1px solid #e2e8f0;
  }

  .main-content {
    padding: 20px !important;
  }

  .page-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 15px;
  }

  .course-card {
    flex-direction: column;
    align-items: flex-start;
    gap: 15px;

    .course-actions {
      width: 100%;
      justify-content: flex-end;
    }
  }

  .course-dialog {
    width: 90% !important;
  }
}
</style>
